package com.juyu.miliao.api.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.UserPhoto;
import com.juyu.miliao.api.service.PrivatePhotoService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.api.validator.UserPhotoCheck;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;

/**
 * 私照接口
 * 
 * @author wbf
 */
@RestController
@RequestMapping("/privatePhoto")
public class PrivatePhotoController {

	@Resource
	PrivatePhotoService privatePhotoService;

	/**
	 * 获取用户私照
	 * @param userId
	 * @return
	 */
	@GetMapping("/{id:^[1-9][0-9]*$}")
	@JsonView(JacksonView.PrivatePhoto.class)
	public List<UserPhoto> getPrivatePhotoById(Authentication token, @PathVariable("id") long id) {
		Subject subject = (Subject) token.getPrincipal();
		return privatePhotoService.getPrivatePhotoById(id, subject.getId());
	}

	/**
	 * 新增用户私照-单张
	 */
	@PostMapping()
	@JsonView(JacksonView.PrivatePhoto.class)
	public UserPhoto addPrivatePhoto(@Validated(UserPhotoCheck.class) UserPhoto entity) {
		return privatePhotoService.addPrivatePhoto(entity);
	}

	/**
	 * 新增用户私照-多张
	 */
	@PostMapping("/batch")
	@JsonView(JacksonView.PrivatePhoto.class)
	public List<UserPhoto> batchAddPrivatePhoto(@Validated(UserPhotoCheck.class) UserPhoto entity) {
			return privatePhotoService.batchAddPrivatePhoto(entity);
	}
//
//	/**
//	 * 设为私密或解除私密
//	 * @param picType
//	 */
//	@PutMapping("/{privateId}-{picType}/picType")
//	public void updatePrivatePhotoPicType(@PathVariable("picType") int picType, @PathVariable("privateId") int privateId){
//		privatePhotoService.updatePrivatePhotoPicType(picType,privateId);
//	}
//

	/**
	 * 删除用户私照
	 */
	@DeleteMapping("/{userId}-{privateId}")
	public void deletePrivatePhoto(@PathVariable("userId") int userId, @PathVariable("privateId") int privateId) {
		privatePhotoService.deletePrivatePhoto(userId, privateId);
	}

	/**
	 * 购买用户私照
	 * 
	 * @param buyUserId
	 *            照片购买者用户id
	 * @param privateId
	 *            私照id
	 * @return
	 */
	@GetMapping("/{buyUserId}-{privateId}")
	public void buyPrivatePhoto(Authentication token, @PathVariable("buyUserId") int buyUserId,
			@PathVariable("privateId") int privateId) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != buyUserId)
			throw new IllegalParamException("用户id错误");
		privatePhotoService.buyPrivatePhoto(buyUserId, privateId);
	}
	/**
	 * 购买用户聊天付费闪图
	 * 
	 * @param buyUserId
	 *            照片购买者用户id
	 * @param inUserId
	 *            照片拥有者 id
	 * @param type
	 * 	 *        图片type=1 语音type=2
	 * @return
	 */
	@GetMapping("/{buyUserId}-{inUserId}/photo")
    public void buyChatPhoto(Authentication token, @PathVariable("buyUserId") int buyUserId,
							 @PathVariable("inUserId") int inUserId, @RequestParam(value="type",required = false,defaultValue = "1") Integer type){
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != buyUserId){
			throw new IllegalParamException("用户id错误");
		}
		privatePhotoService.buyChatPhoto(buyUserId, inUserId,type);
    }

}
